package com.eric.controller;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.bean.BeanUtil;
import com.eric.service.IUserPermissionService;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 用户权限表 前端控制器
 * </p>
 *
 * @author 新哥
 * @since 2024-03-12
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/user-permission")
public class UserPermissionController {
    private final IUserPermissionService userPermissionService;

    @PostMapping("/saveUserPermission")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult saveUserPermission(@RequestBody Map<String, List<Integer>>paramMaps){

        if (BeanUtil.isEmpty(paramMaps)){
            return SaResult.error("输入错误，请检查后再输入");
        }
        return userPermissionService.saveUserPermission(paramMaps);
    }

    //    删除一个用户的1~n个Permission权限
    @DeleteMapping("/deleteUserPermission")
    @SaCheckPermission("sys:delete")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    public SaResult deleteUserPermission(@RequestBody Map<String,List<Integer>>ids){
        if (ids.isEmpty()){
            return SaResult.error("数据为空，请检查后再此提交");
        }
        return userPermissionService.deleteUserPermissionsService(ids);
    }

//    分页获取所有的信息
    @GetMapping("/getAll")
    public SaResult getAll(Integer current,Integer pageSize){
        return userPermissionService.getAllService(current,pageSize);
    }
//    通过userId获取多项信息
    @GetMapping("/getByUserId")
    public SaResult getByUserId(Integer userId){
        return userPermissionService.getByUserIdService(userId);
    }

    @GetMapping("/{id}")
    public SaResult getById(@PathVariable("id")Integer id){
        return userPermissionService.getByIdService(id);
    }
    @GetMapping("/getByPermissionId")
    public SaResult getByPermissionId(Integer permissionId,Integer current,Integer pageSize){
        return userPermissionService.getByPermissionIdService(permissionId,current,pageSize);
    }

    @GetMapping("/getLoginUserPermissionList")
    public SaResult getLoginUserPermissionList(){
        List<String> permissionList = StpUtil.getPermissionList();
        return SaResult.data(permissionList);
    }

    @PutMapping("/updateUserPermission")
    @SaCheckPermission("sys:modify")
    @SaCheckRole(value = {"sys:coder","sys:superAdmin","sys:admin"},mode = SaMode.OR)
    @Transactional
    public SaResult updateUserPermission(@RequestBody Map<String, List<Integer>>paramMaps){
        return userPermissionService.updateUserPermission(paramMaps);
    }

}
